﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FastShip88.Admin
{
    public partial class AdmParcelRecord : System.Web.UI.Page
    {
        DBAccess.UsersDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBAccess.UsersDataContext();

            if (!Page.IsPostBack)
            {
                init_page();

            }

            lblSaveResult.Text = "";


        }

        private void init_page()
        {
            //throw new NotImplementedException();
            txtParcelNo.Text="";
            txtUserName.Text="";
            txtWeight.Text= "";
            txtDescription.Text= "";
            txtMemo.Text= "";
            txtLength.Text= "";
            txtWidth.Text="";
            txtHeight.Text="";

        }

        protected void btnCheckParcel_Click(object sender, EventArgs e)
        {
            string strParcelNo = txtParcelNo.Text.Trim();

            var q_parcels = from p in db.ParcelInfos
                            where p.ParcelNo == strParcelNo
                            select p;
            if (q_parcels.Count() > 0)
            {
                lblCheckParcelResult.Text = "该包裹已存在！";
            }
            else
            {
                lblCheckParcelResult.Text = "该包裹不存在。";
            }
        }

        protected void btnCheckUser_Click(object sender, EventArgs e)
        {
            string strUserName = txtUserName.Text.Trim();

            int userId = Utility.GetUserIdByUserName(strUserName);

            if (userId > 0)
            {
                var q_user = (from u in db.UserInfos
                              where u.Id == userId
                              select u).First();
                if (q_user.LastName.Trim() + q_user.FirstName.Trim() != "")
                    lblCheckUserResult.Text = "该用户名称为：" + q_user.LastName + " " + q_user.FirstName;
                else
                    lblCheckUserResult.Text = "该用户存在。";
            }
            else
            {
                lblCheckUserResult.Text = "该用户不存在，请重新输入！";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.IsValid)
            {
                return;
            }

            var pi = new DBAccess.ParcelInfo();

            pi.ParcelNo = txtParcelNo.Text.Trim();
            pi.UserId = Utility.GetUserIdByUserName(txtUserName.Text.Trim());
            pi.ShopId = SafeConvert.ToInt( ddlShops.SelectedValue);
            pi.DeliveryComId = (byte?) SafeConvert.ToIntOrNull(ddlDeliveryCom.SelectedValue);
            pi.StatusId = 10;
            pi.Weight = SafeConvert.ToDouble(txtWeight.Text);
            pi.Description = txtDescription.Text.Trim();
            pi.Memo = txtMemo.Text.Trim();
            pi.Length = SafeConvert.ToIntOrNull(txtLength.Text);
            pi.Width = SafeConvert.ToIntOrNull(txtWidth.Text);
            pi.Height = SafeConvert.ToIntOrNull(txtHeight.Text);
            pi.ArriveDate = DateTime.Today;

            db.ParcelInfos.InsertOnSubmit(pi);
            db.SubmitChanges();

            Utility.SendEmail(Utility.EmailTypes.ParcelInboundNotification, pi.UserId.Value, pi.ParcelNo);
            init_page();
            lblSaveResult.Text = "已成功添加记录。";

        }

        protected void vldCheckParcel_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string strParcelNo = txtParcelNo.Text.Trim();

            var q_parcels = from p in db.ParcelInfos
                            where p.ParcelNo == strParcelNo
                            select p;
            if (q_parcels.Count() > 0)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }

        }

        protected void vldCheckUser_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string strUserName = txtUserName.Text.Trim();

            int userId = Utility.GetUserIdByUserName(strUserName);

            if (userId > 0)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }

        }
    }
}
